package DataAccess;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import pojo.LogThaoTacPOJO;

public class LogThaoTacDA extends BaseDA {

	@Override
	protected LogThaoTacPOJO getProperties(ResultSet rss) throws SQLException {

		LogThaoTacPOJO rs = new LogThaoTacPOJO();
		rs.setId(rss.getInt("id"));
		rs.setNguoiDung(rss.getString("nguoidung"));
		rs.setHanhDong(rss.getString("hanhdong"));
		rs.setGhiChu(rss.getString("ghichu"));
		rs.setIdCay(rss.getInt("idcay"));
		rs.setThoiGian(rss.getTimestamp("thoigian").getTime());
		return rs;
	}

	public ArrayList<LogThaoTacPOJO> getByIdCay(int idCay) {

		ArrayList<LogThaoTacPOJO> rs = new ArrayList<LogThaoTacPOJO>();
		try {
			String sql = "select log.*, loai.ten as hanhdong from logthaotac log join loailog loai on log.loai=loai.ma where idcay="+idCay+" order by id";
			ResultSet rss = provider.executeQuery(sql);
			while (rss.next()) {
				LogThaoTacPOJO log = getProperties(rss);
				rs.add(log);
			}
		} catch (Exception e) {
			rs = null;
		}
		return rs;
	}

	public int insert(LogThaoTacPOJO log) {		
		// sql="insert into logthaotac(nguoidung,hanhdong,ghichu,idcay) values('phu','set quyen','ghi chu',1);";
		String sql = "insert into logthaotac(nguoidung,ghichu,idcay,loai) values(?,?,?,?);";
		String[] input = new String[] { log.getNguoiDung(), log.getGhiChu(), log.getIdCay() + "",log.getLoai()+"" };
		int rs = provider.execPrepareUpdate(sql, input);
		return rs;
	}

	public int insert(String nguoiDung, String ghiChu, int idCay, int loai ){
		LogThaoTacPOJO log=new LogThaoTacPOJO(nguoiDung,ghiChu,idCay,loai);
		return insert(log);
	}
}
